package legumes.client.ui.widgets.form.item;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.validator.CustomValidator;
import com.smartgwt.client.widgets.form.validator.LengthRangeValidator;

public class PhoneFormItem extends TextItem {

	public PhoneFormItem(String title_, String phoneNumberExampleValue_, boolean mandatory_){
		super("PhoneFormItem", title_);
		setShowTitle(true);
		setHint("<i>Exemple : '" + phoneNumberExampleValue_ + "'</i>");
		setWidth(200);
		setTextAlign(Alignment.LEFT);
		setValidateOnExit(true);
		setValidateOnChange(true);
		
		LengthRangeValidator lrv = new LengthRangeValidator();
        lrv.setMin(1);
        lrv.setMax(20);
        lrv.setErrorMessage("Saisir un numéro ayant entre 1 et 20 charact&egrave;res.");
        
        if(mandatory_){
	        CustomValidator cv = new CustomValidator(){
	
				@Override
				protected boolean condition(Object value) {
					return value instanceof String && 
					value != null &&
					((String)value).trim().length()>0;
				}
	        	
	        };
	        cv.setErrorMessage("Veuillez saisir au moins un caractère.");
	
	        setValidators(lrv, cv);
        } else {
        	setValidators(lrv);
        }
	}
}
